package com.adinnet.admin.product.service.impl;

import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.product.model.Product;
import com.adinnet.admin.product.mapper.ProductMapper;
import com.adinnet.admin.product.model.form.ProductForm;
import com.adinnet.admin.product.model.vo.ProductReq;
import com.adinnet.admin.product.model.vo.ProductResp;
import com.adinnet.admin.product.service.ProductService;
import com.adinnet.admin.productImg.mapper.ProductImgMapper;
import com.adinnet.admin.productImg.model.ProductImg;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author liukp
 * @since 2020-07-02
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private ProductImgMapper productImgMapper;

    /**
     * 商品列表
     * @param productReq
     * @return
     */
    @Override
    public PageResult<ProductResp> list(ProductReq productReq) {
        SysUserInfo sysUserInfo = commonService.getLoginUser();
//        if (sysUse1r != null && sysUser.getStoreId().intValue() > 0) {
//            productReq.setStoreId(sysUser.getStoreId());
//        }

        Page<ProductResp> iPage = new Page<>(productReq.getPage(), productReq.getLimit());
        List<ProductResp> productList = baseMapper.list(iPage, productReq);
        return new PageResult(productList, productReq.getTotal());
    }

    /**
     * 新增商品
     * @param productForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addProduct(ProductForm productForm, HttpServletRequest request) {
        productForm.setPicNum(StringUtils.isNotEmpty(productForm.getImages()) ? productForm.getImages().split(",").length : 0);
        baseMapper.insert(productForm);
        if(StringUtils.isNotEmpty(productForm.getImages())){
            String[] images = productForm.getImages().split(",");
            for (String image : images) {
                ProductImg img = new ProductImg();
                img.setImgUrl(image);
                img.setProductId(productForm.getId());
                productImgMapper.insert(img);
            }
        }
        commonService.saveOperation(request, "新增商品，ID：" + productForm.getId());
        return "添加成功";
    }

    /**
     * 修改商品
     *
     * @param productForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String updateProduct(ProductForm productForm, HttpServletRequest request) {
        productForm.setPicNum(StringUtils.isNotEmpty(productForm.getImages()) ? productForm.getImages().split(",").length : 0);
        baseMapper.updateById(productForm);
        productImgMapper.delProductImg(productForm.getId());
        if (StringUtils.isNotEmpty(productForm.getImages())) {
            for (String image : productForm.getImages().split(",")) {
                ProductImg img = new ProductImg();
                img.setImgUrl(image);
                img.setProductId(productForm.getId());
                productImgMapper.insert(img);
            }
        }
        commonService.saveOperation(request, "修改商品，ID：" + productForm.getId());
        return "修改成功";
    }

    /**
     *商品下架/上架
     * @param id
     * @param status
     * @return
     */
    @Override
    public String solOutProduct(Integer id,Integer status){
        baseMapper.solOutProduct(id,status);
        return "操作成功";
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @Override
    @Transactional
    public String delProduct(Integer id, HttpServletRequest request){
        baseMapper.delProduct(id);
        productImgMapper.delProductImg(id);
        commonService.saveOperation(request, "删除商品，ID：" + id);
        return "操作成功";
    }
}
